/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package xmarti.vacaxm.client.sequence;

import java.util.logging.Level;
import java.util.logging.Logger;
import xmarti.vacaxm.action.ImplSequence;
import xmarti.vacaxm.beans.sequence.SequenceType;
import xmarti.vacaxm.exception.ExceptionHibernateVacaxm;
import xmarti.vacaxm.utils.TDSLanguageUtils;

/**
 *
 * @author xmarti
 */
public class JDSequenceType extends javax.swing.JDialog {
    private ImplSequence aSequence; 
    private SequenceType sequenceType;
    public JDSequenceType(java.awt.Frame parent, boolean modal, SequenceType sequenceType) {
        super(parent, modal);
        this.sequenceType= sequenceType; 
        this.aSequence= new ImplSequence();
        initComponents();
        if (this.sequenceType.getId()==-1){
            this.setTitle(TDSLanguageUtils.getMessage("jDialogSequenceType.new.caption"));
        }else{
            this.setTitle(TDSLanguageUtils.getMessage("jDialogSequenceType.modify.caption"));
        }
        setData();
    }
    private void getData(){
        this.sequenceType.setName(this.jTextName.getText());
        this.sequenceType.setDescription(this.jTextDescription.getText());        
    }
    private void setData(){
        this.jTextName.setText(this.sequenceType.getName());
        this.jTextDescription.setText(this.sequenceType.getDescription());
    }
    private void saveData() throws ExceptionHibernateVacaxm{
        getData();
        if (this.sequenceType.getId()==-1){            
            this.aSequence.add(this.sequenceType);
            this.sequenceType= this.aSequence.getSequenceTypeByName(this.sequenceType.getName());
        }else{
            this.aSequence.update(this.sequenceType);
        }
            
    }
    public SequenceType getSequenceType(){
        return this.sequenceType;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelCaptionName = new javax.swing.JLabel();
        jTextName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextDescription = new javax.swing.JTextArea();
        jLabelDescription = new javax.swing.JLabel();
        jButtonSave = new javax.swing.JButton();
        jButtonExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabelCaptionName.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabelCaptionName.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelCaptionName.setText(TDSLanguageUtils.getMessage("jDialogSequenceType.JLabel.name.caption"));

        jTextName.setBackground(new java.awt.Color(255, 255, 204));
        jTextName.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jTextName.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextName.setToolTipText(TDSLanguageUtils.getMessage("jPanelAnimalData.jTextName.toolTipText"));
        jTextName.setAlignmentX(0.0F);
        jTextName.setAlignmentY(0.0F);

        jTextDescription.setColumns(20);
        jTextDescription.setRows(5);
        jScrollPane1.setViewportView(jTextDescription);

        jLabelDescription.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabelDescription.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelDescription.setText(TDSLanguageUtils.getMessage("jDialogSequenceType.JLabel.description.caption"));

        jButtonSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xmarti/vacaxm/image/BotoGuardar.png"))); // NOI18N
        jButtonSave.setText(TDSLanguageUtils.getMessage("jDialogSequenceType.JButton.save.caption"));
        jButtonSave.setToolTipText(TDSLanguageUtils.getMessage("jDialogSequenceType.JButton.save.toolTipText"));
        jButtonSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        jButtonExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xmarti/vacaxm/image/BotoSortir.png"))); // NOI18N
        jButtonExit.setText(TDSLanguageUtils.getMessage("jDialogSequenceType.JButton.exit.caption"));
        jButtonExit.setToolTipText(TDSLanguageUtils.getMessage("jDialogSequenceType.JButton.exit.toolTipText"));
        jButtonExit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonExit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelCaptionName, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextName, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonSave, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextName, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelCaptionName, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonSave)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
        try {
            saveData();            
        } catch (ExceptionHibernateVacaxm ex) {
            Logger.getLogger(JDSequenceType.class.getName()).log(Level.SEVERE, null, ex);
          
        }finally{
              this.dispose(); 
        }    
    }//GEN-LAST:event_jButtonSaveActionPerformed

    private void jButtonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExitActionPerformed
        this.dispose(); 
    }//GEN-LAST:event_jButtonExitActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(JDSequenceType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(JDSequenceType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(JDSequenceType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(JDSequenceType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
////                JDSequenceType dialog = new JDSequenceType(new javax.swing.JFrame(), true);
////                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
////                    @Override
////                    public void windowClosing(java.awt.event.WindowEvent e) {
////                        System.exit(0);
////                    }
////                });
////                dialog.setVisible(true);
//            }
//        });
  //  }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JLabel jLabelCaptionName;
    private javax.swing.JLabel jLabelDescription;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextDescription;
    private javax.swing.JTextField jTextName;
    // End of variables declaration//GEN-END:variables
}
